import numpy as np


input_example = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1]]

output_example = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1]]



# def compare(o_e, o):
#     assert isinstance(o_e, np.ndarray)
#     assert isinstance(o, np.ndarray)
#
#     ori = 0
#     cur = 0
#     no = o_e.shape[1]
#     for num in range(0, no):
#         ori = ori + np.var(o_e[:, num])
#
#     for num in range(0, no):
#         cur = cur + np.var(o[:, num])
#
#     return ori, cur, cur < ori


def compare(o_e, o):
    assert isinstance(o_e, np.ndarray)
    assert isinstance(o, np.ndarray)
    if o_e.shape == o.shape:

        temp = o_e == o
        flag = True
        for row in temp:
            for col in row:
                flag = flag & col
        return flag
    else:
        return False


def ourpreprocess(temp_arr):
    no=temp_arr.shape[1]
    DSIZE=temp_arr.shape[0]
    for num in range(0,no):
        value=temp_arr[:,num]
        npArray=dataPreprocess(value,DSIZE)
        temp_arr[:, num]=npArray
    return temp_arr


def percent_range(dataset,DSIZE, min=0.20, max=0.80):
    range_max = np.percentile(dataset, max * 100)
    range_min = -np.percentile(-dataset, (1 - min) * 100)

    result=np.empty((DSIZE,))
    i=0
    for value in dataset:
        if value <= range_max and value >= range_min:
            result[i]=dataset[i]
        else:
            result[i]=-1

        i+=1
    return result


def dataPreprocess(npArray,DSIZE):
    npArray = percent_range(npArray, DSIZE, 0.025, 0.975)
    return npArray


# 用例目的
print("该用例目的为：")
print("进行离群点处理组件功能测试，测试该组件在边界条件下的表现，该处理后矩阵经给定方差计算方法计算，方差等于原数据方差")

# 子用例编号
print("子用例编号：")
print("Outlier_3")

print("****************************")
print("当前输入为：")
# 输出用例设置
print(input_example)
# 输出用例设置

print("")

print("****************************")
print("当前输出为:")
# 输出处理后数据

output = ourpreprocess(np.array(input_example))
print(output)
# 输出处理后数据

print("****************************")
print("是否正确:")
# 输出对比结果
# 需要写一个compare函数
data = compare(np.array(output_example), output)
if data:
    # print(f"原数组方差为：${data[0]}")
    # print(f"当前数组方差为：${data[1]}，小于原始数组")
    print("输出与预定目标相符")
else:
    print("输出与预定目标不符")
# 输出对比结果

print("\n")
